package hu.elte.tudaskezelo.dbpedialike.activity;

import hu.elte.tudaskezelo.dbpedialike.R;
import hu.elte.tudaskezelo.dbpedialike.views.overlay.OverlayFactory;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import roboguice.activity.RoboActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.google.inject.Inject;

public class DBpediaLikeActivity extends RoboActivity implements
		LocationListener, OnTouchListener {

	final String tag = "DBpediaLikeActivity";

	@Inject
	protected LocationManager locationManager;
	@Inject
	protected OverlayFactory overlayFactory;

	protected MapView openStreetMapView;
	protected MyLocationOverlay myLocationOverlay;

	private boolean followMyLocation;
	private boolean zoom;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(tag, "onCreate");
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		openStreetMapView = (MapView) findViewById(R.id.mapview);
		openStreetMapView.getController().setZoom(3);
		openStreetMapView.setOnTouchListener(this);

		myLocationOverlay = new MyLocationOverlay(this.getApplicationContext(),
				openStreetMapView);
		openStreetMapView.getOverlays().add(this.myLocationOverlay);
		// openStreetMapView.getOverlayManager().add(myLocationOverlay);
		myLocationOverlay.enableMyLocation();
		openStreetMapView.postInvalidate();

		// openStreetMapView.setBuiltInZoomControls(true);
		followMyLocation = true;
		drawPOI = false;
		zoom = false;

	}

	CountDownTimer countDownTimer;
	float xPixel;
	float yPixel;
	boolean drawPOI;
	Context timerContext;

	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();

		if (action == MotionEvent.ACTION_DOWN) {
			xPixel = event.getX();
			yPixel = event.getY();
			timerContext = this.getApplicationContext();

			countDownTimer = new CountDownTimer(1000, 1000) {

				@Override
				public void onTick(long millisUntilFinished) {
				}

				@Override
				public void onFinish() {
					IGeoPoint fromPixels = openStreetMapView.getProjection()
							.fromPixels(xPixel, yPixel);
					if (!drawPOI) {
						// IGeoPoint fromPixels = openStreetMapView
						// .getProjection().fromPixels(xPixel, yPixel);
						showCityInformation(
								fromPixels.getLongitudeE6() * 0.000001,
								fromPixels.getLatitudeE6() * 0.000001);
					} else {
						drawPOI = false;
						Log.d(tag, "Overlay");
						ItemizedOverlay<OverlayItem> ItemizedOverlay = overlayFactory
								.getLocal(timerContext,
										fromPixels.getLongitudeE6() * 0.000001,
										fromPixels.getLatitudeE6() * 0.000001,
										2, null);
						openStreetMapView.getOverlayManager().clear();
						openStreetMapView.getOverlayManager().add(
								ItemizedOverlay);
						openStreetMapView.invalidate();
					}
				}
			}.start();
		}

		if (action == MotionEvent.ACTION_MOVE
				|| action == MotionEvent.ACTION_UP) {
			countDownTimer.cancel();
		}

		return true;
	}

	public void showCityInformation(double longitude, double latitude) {
		Intent intent;
		intent = new Intent().setClass(this, ViewCityInformationActivity.class);
		intent.putExtra("longitude", longitude);
		intent.putExtra("latitude", latitude);
		startActivity(intent);
	}

	@Override
	protected void onResume() {
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, this);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		locationManager.removeUpdates(this);
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		locationManager.removeUpdates(this);
		super.onDestroy();
	}

	@Override
	public void onLocationChanged(Location location) {
		if (followMyLocation) {
			openStreetMapView.getController().setCenter(
					new GeoPoint(location.getLatitude(), location
							.getLongitude()));
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	private static final int MENU_ZOOM = 0;
	private static final int MENU_FOLLOW = 1;
	private static final int MENU_GETPOI = 2;
	private static final int MENU_FINDCITY = 3;

	// private static final int MENU_SETZOOM = 2;

	// private static final int MENU_MAP_MODE = 2;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_ZOOM, 0, R.string.zoom).setIcon(
				android.R.drawable.ic_menu_zoom);
		menu.add(0, MENU_FOLLOW, 0, R.string.followMyLocation).setIcon(
				android.R.drawable.ic_menu_mylocation);
		menu.add(0, MENU_GETPOI, 0, R.string.getPOI).setIcon(
				android.R.drawable.ic_menu_mylocation);
		menu.add(0, MENU_FINDCITY, 0, R.string.searchCity).setIcon(
				android.R.drawable.ic_menu_mylocation);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ZOOM:
			return menuZoom();
		case MENU_FOLLOW:
			return menuFollow();
		case MENU_GETPOI:
			return getPOI();
		case MENU_FINDCITY:
			return findCity();
		}

		return false;
	}

	private boolean findCity() {
		Intent intent = new Intent(getApplicationContext(),
				FindCityActivity.class);
		startActivityForResult(intent, MENU_FINDCITY);

		return false;
	}

	private boolean getPOI() {
		drawPOI = true;
		Log.d(tag, "DrawPoi: " + drawPOI);
		return true;
	}

	private boolean menuFollow() {
		// TODO Auto-generated method stub
		return true;
	}

	private boolean menuZoom() {
		zoom = !zoom;
		return true;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == MENU_FINDCITY) {
			if (resultCode == 2) {

				Double longitude = data.getDoubleExtra("longitude", 0);
				Double latitude = data.getDoubleExtra("latitude", 0);

				Log.d(tag+" onActivityResult", "longitude: " + longitude);
				Log.d(tag+" onActivityResult", "latitude: " + latitude);
				openStreetMapView.getController().setCenter(
						new GeoPoint(longitude,latitude ));
				openStreetMapView.getController().setZoom(5);
			}
		}

	}

}